package com.mzkj.wanjulaile.mobile.api.modules.weChat.controller;

import com.mzkj.common.utils.StringUtils;
import com.mzkj.wanjulaile.mobile.api.common.controller.BaseController;
import com.mzkj.wanjulaile.mobile.api.modules.weChat.utils.WechatUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * Created by wangxw on 2016/10/23.
 */
@Controller
public class WechatController extends BaseController {

//    @Resource
//    private WechatService wechatService;
//    @Autowired
//    private SendMsgService sendMsgService;

    @RequestMapping(value = "/m/wechat", method = RequestMethod.GET)
    public void weChat(HttpServletRequest request, HttpServletResponse response) {
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        System.out.println("-----------------------验证消息(GET)------------------------");
        try {
            if (StringUtils.isNotEmpty(signature) && StringUtils.isNotEmpty(timestamp)
                    && StringUtils.isNotEmpty(nonce)) {
                PrintWriter out = response.getWriter();
                if (WechatUtils.checkSignature(signature, timestamp, nonce)) {
                    out.print(echostr);
                }
                out.close();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

//    @RequestMapping(value = "/m/wechat",method = RequestMethod.POST)
//    public void receiveMessage(HttpServletRequest request,HttpServletResponse response){
//        try {
//            System.out.println("------------------------处理消息(POST)---------------------");
//            //设置请求&相应的编码为utf-8
//            request.setCharacterEncoding("UTF-8");
//            response.setCharacterEncoding("UTF-8");
//            //处理消息
//            String resMessage = wechatService.processRequest(request);
//            PrintWriter out = response.getWriter();
//            if(StringUtils.isEmpty(resMessage)) {
//                out.print("success");
//            }else {
//                out.print(resMessage);
//            }
//            out.close();
//        } catch (Exception e) {
//            logger.error(e.getMessage(),e);
//        }
//    }

    /*****************************************************************************
     *				已上代码为微信服务器验证代码,请勿随意修改                         *
     /*****************************************************************************/

}
